//
//  HACCrashSavior.m
//  HACRouter
//
//  Created by Chengjie on 2019/5/25.
//

#import "HACCrashSavior.h"
#import <objc/runtime.h>


/**
 拯救函数
 */
static id hac_saviorImp(id target_, SEL sel_, ...) {
    return nil;
}

static const char *hac_getArgsTypes(SEL sel_) {
    NSString *selName = NSStringFromSelector(sel_);
    NSString *nonSelName = [selName stringByReplacingOccurrencesOfString:@":" withString:@""];
    NSString *argsTypes = @"@@:";
    NSUInteger argsCount = selName.length - nonSelName.length;
    while (argsCount) {
        argsTypes = [argsTypes stringByAppendingString:@"@"];
        argsCount--;
    }
    return [argsTypes UTF8String];
}

@implementation HACCrashSavior

- (BOOL)hac_addInstanceSaviorSel:(SEL)sel_ {
    Class cls_ = [HACCrashSavior class];
    return class_addMethod(cls_, sel_, (IMP)hac_saviorImp, hac_getArgsTypes(sel_));
}

+ (BOOL)hac_addClassSaviorSel:(SEL)sel_ {
    Class cls_ = object_getClass([HACCrashSavior class]);
    return class_addMethod(cls_, sel_, (IMP)hac_saviorImp, hac_getArgsTypes(sel_));
}

@end
